package com.gullmall.coupon.service.impl;

import com.gullmall.common.to.MemberPrice;
import com.gullmall.common.to.SkuDiscountTo;
import com.gullmall.common.utils.R;
import com.gullmall.coupon.entity.MemberPriceEntity;
import com.gullmall.coupon.entity.SkuLadderEntity;
import com.gullmall.coupon.service.MemberPriceService;
import com.gullmall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gullmall.common.utils.PageUtils;
import com.gullmall.common.utils.Query;

import com.gullmall.coupon.dao.SkuFullReductionDao;
import com.gullmall.coupon.entity.SkuFullReductionEntity;
import com.gullmall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
@Transactional
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveSkuDiscount(SkuDiscountTo skuDiscountTo) {
        //1, 保存

        //2 sms_sku_ladder(满几件打几折)
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuDiscountTo.getSkuId());
        skuLadderEntity.setFullCount(skuDiscountTo.getFullCount());//满几件打折
        skuLadderEntity.setDiscount(skuDiscountTo.getDiscount());//打几折
        skuLadderEntity.setAddOther(skuDiscountTo.getCountStatus());//是否参与其他优惠
        if (skuLadderEntity.getFullCount()>0 && skuLadderEntity.getDiscount().compareTo(new BigDecimal(0))==1 ){
            skuLadderService.save(skuLadderEntity);
        }

        //3 sms_sku_full_reduction(满多少钱打几折)
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuDiscountTo.getSkuId());
        skuFullReductionEntity.setFullPrice(skuDiscountTo.getFullPrice());//满多少钱打折
        skuFullReductionEntity.setReducePrice(skuDiscountTo.getReducePrice());//打几折
        skuFullReductionEntity.setAddOther(skuDiscountTo.getPriceStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1 && skuFullReductionEntity.getReducePrice().compareTo(new BigDecimal(0))==1){
            this.save(skuFullReductionEntity);
        }


        //4 sms_member_price(会员打折表)
        List<MemberPrice> memberPrice = skuDiscountTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities= memberPrice.stream().
                filter(men->men.getPrice().compareTo(new BigDecimal(0))==1)
                .map(mem -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setMemberLevelId(mem.getId());//会员等级id,gullmall_ums库下的ums_member_level表中
            memberPriceEntity.setMemberLevelName(mem.getName());//会员等级名称
            memberPriceEntity.setMemberPrice(mem.getPrice());//会员价格
            memberPriceEntity.setSkuId(skuDiscountTo.getSkuId());
            memberPriceEntity.setAddOther(1);//默认可叠加其他优惠
            return memberPriceEntity;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(memberPriceEntities);

        return R.ok();
    }

}